//
//  SendMessageView.m
//  Yihubang
//
//  Created by xmy on 16/9/7.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "SendMessageView.h"
#import "DXFaceView.h"
@interface SendMessageView()<DXFaceDelegate,UITextViewDelegate>
@property (nonatomic , strong) DXFaceView * faceView;
@end

@implementation SendMessageView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)awakeFromNib{
    [super awakeFromNib];
    @weakify(self);
    [self.backView setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self);
        [self removeFrom:NO];
    }];
    [self.faceViewShow_btn setImage:[UIImage imageNamed:@"chatBar_face"] forState:UIControlStateNormal];
    [self.faceViewShow_btn addTarget:self action:@selector(faceViewShow:) forControlEvents:UIControlEventTouchUpInside];
    [self.sendMessage addTarget:self action:@selector(sendMessageAction) forControlEvents:UIControlEventTouchUpInside];
    self.message_view.delegate =self;
    self.message_view.returnKeyType=UIReturnKeySend;
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5/*延迟执行时间*/ * NSEC_PER_SEC));
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
         @strongify(self);
        self.faceView = [[DXFaceView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, 216)];
        [self.faceView setDelegate:self];
        [self.faceImgView addSubview:self.faceView];
    });

    self.message_view.placeholderStr =@"说点什么吧";
}
-(void)removeFrom:(BOOL)success{
     @weakify(self);
    [UIView animateWithDuration:0.4 animations:^{
        @strongify(self);
        self.alpha=0.0f;
    } completion:^(BOOL finish){
        if(success){
             @strongify(self);
            if(_delegate){
                [self.delegate sendSuccessThisMessage:success];
            }
        }
        [self removeFromSuperview];
        if([self.message_view isFirstResponder]){
            [self.message_view resignFirstResponder];
        }
    }];
}

-(void)faceViewShow:(id)sender{
    if([self.message_view isFirstResponder]){
        [self.message_view resignFirstResponder];
    }
    [self faceViewShowbefore];
}

-(void)faceViewHidden:(id)sender{
    [self.message_view becomeFirstResponder];
    [self faceViewHiddenAfter];
}
-(void)faceViewHiddenAfter{
    self.faceView_height.constant =-216.f;
    self.textView_bottom.constant =0.f;
    @weakify(self);
    [UIView animateWithDuration:0.4 animations:^{
        @strongify(self);
        [self.faceImgView layoutIfNeeded];
        [self.SendTextView layoutIfNeeded];
        [self layoutIfNeeded];
    } completion:^(BOOL finish){
        @strongify(self);
        [self.faceViewShow_btn setImage:[UIImage imageNamed:@"chatBar_face"] forState:UIControlStateSelected];
        [self.faceViewShow_btn removeTarget:self action:@selector(faceViewHidden:) forControlEvents:UIControlEventTouchUpInside];
        [self.faceViewShow_btn addTarget:self action:@selector(faceViewShow:) forControlEvents:UIControlEventTouchUpInside];
    }];
}
-(void)faceViewShowbefore{
    self.faceView_height.constant =0.f;
    self.textView_bottom.constant =216.f;
    @weakify(self);
    [UIView animateWithDuration:0.4 animations:^{
        @strongify(self);
        [self.faceImgView layoutIfNeeded];
        [self.SendTextView layoutIfNeeded];
        [self layoutIfNeeded];
    } completion:^(BOOL finish){
        @strongify(self);
        [self.faceViewShow_btn setImage:[UIImage imageNamed:@"chatBar_keyboard"] forState:UIControlStateSelected];
        [self.faceViewShow_btn removeTarget:self action:@selector(faceViewShow:) forControlEvents:UIControlEventTouchUpInside];
        [self.faceViewShow_btn addTarget:self action:@selector(faceViewHidden:) forControlEvents:UIControlEventTouchUpInside];
    }];
}
-(void)sendMessageAction{
    self.sendMessage.enabled = NO;
    switch (self.commendType) {
        case CommendType_News:
            [self commentNews :self.message_view.text newsId:self.sbInfo];
            break;
        case CommendType_Help:
            [self commentHelp :self.message_view.text helpId:self.sbInfo];
            break;
        case CommendType_Cule:
            [self commentClue:self.message_view.text clueId:self.sbInfo];
            break;
        case CommendType_News_reply:
            [self replyNewsComment];
            break;
        case CommendType_Help_Reply:
            [self replyHelpComment];
            break;
        default:
            [self replyClueComment];
            break;
    }
}

-(void)commentNews:(NSString *)text newsId:(NSString *)sb_id{
    @weakify(self);
    [[[HttpManagerCenter sharedHttpManager] commentWithNewId:sb_id content:text uid:nil  resultClass:nil] subscribeNext:^(BaseModel *model) {
         @strongify(self);
        if (model.code==200) {
            [self.superViewController hiddenHUDWithString:@"评论成功" error:YES];
            [self removeFrom:YES];
        }else{
            [self.superViewController hiddenHUDWithString:model.message error:YES];
            self.sendMessage.enabled = YES;
        }
    }];
}

-(void)commentHelp:(NSString *)text helpId:(NSString *)sb_id{
     @weakify(self);
    [[[HttpManagerCenter sharedHttpManager] commentWithContent:text byHelpId:sb_id resultClass:nil] subscribeNext:^(BaseModel *model) {
         @strongify(self);
        if (model.code==200) {
            [self.superViewController hiddenHUDWithString:@"评论成功" error:YES];
            [self removeFrom:YES];
        }else{
            [self.superViewController hiddenHUDWithString:model.message error:YES];
            self.sendMessage.enabled = YES;
        }
    }];
}
-(void)commentClue :(NSString *)text clueId:(NSString *)sb_id{
    @weakify(self);
    [[[HttpManagerCenter sharedHttpManager] commentWithContent:text byClueId:sb_id resultClass:nil] subscribeNext:^(BaseModel *model) {
        @strongify(self);
        if (model.code==200) {
            [self.superViewController hiddenHUDWithString:@"评论成功" error:YES];
            [self removeFrom:YES];
        }else{
            [self.superViewController hiddenHUDWithString:model.message error:YES];
            self.sendMessage.enabled = YES;
        }
    }];
}

-(void)replyNewsComment{
    NSString *text = [NSString stringWithFormat:@"回复了%@:%@",self.sbInfo[@"nikename"],self.message_view.text];
    [self commentNews:text newsId:self.sbInfo[@"news_id"]];
}

-(void)replyHelpComment{
    NSString *text = [NSString stringWithFormat:@"回复了%@:%@",self.sbInfo[@"nikename"],self.message_view.text];
    [self commentHelp :text helpId:self.sbInfo[@"help_id"]];
}
-(void)replyClueComment{
     NSString *text = [NSString stringWithFormat:@"回复了%@:%@",self.sbInfo[@"nikename"],self.message_view.text];
    [self commentClue:text clueId:self.sbInfo[@"clue_id"]];
}
#pragma DXFaceDelegate
- (void)selectedFacialView:(NSString *)str isDelete:(BOOL)isDelete
{
    if (isDelete) {
        [self.message_view deleteEmojiValue];
    }else{
        [self.message_view addEmojiValue:str];
    }

}
- (void)sendFace{
        [self sendMessageAction];
}

#pragma UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if(self.textView_bottom.constant ==216.f){
        [self faceViewHiddenAfter];
    }
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        [self sendMessageAction];
        [self.message_view resignFirstResponder];
        return NO;  //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    
    return YES;
}
@end
